package com.huaao.micropolice.core.service.alert.Impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.huaao.micropolice.core.common.page.Page;
import com.huaao.micropolice.core.common.page.Parameter;
import com.huaao.micropolice.core.common.page.Sort;
import com.huaao.micropolice.core.model.alert.ApiAlert;
import com.huaao.micropolice.core.model.police.Alert;
import com.huaao.micropolice.core.repository.alert.AlertDao;
import com.huaao.micropolice.core.service.alert.AlertService;


/**
 * 警情
 * @author zhangyu
 *
 */
@Service
public class AlertServiceImpl  implements AlertService{
	@Autowired
	AlertDao dao;
	
	@Override
	public String saveAlertInfoAlert(ApiAlert alert) {
		return dao.save(alert);
	}

	@Override
	public List<ApiAlert> findAlertInfoListToPage(Page page) {
		return dao.findAlertInfo(null,new Sort(),page);
	}

	@Override
	public ApiAlert findAlertInfoToid(String id) {
		
		return dao.findAlerInfoToid(id);
	}

	@Override
	public List<ApiAlert> findAlertInfoListTouid(String uid,Page page) {
		List<Parameter> params=new ArrayList<>();
		Parameter parameter=new Parameter();
		parameter.setField("uid");
		parameter.ops="and";
		parameter.setValue(uid);
		parameter.setSign("eq");
		params.add(parameter);
		return dao.findAlertInfo(params,new Sort(),page);
	}

	
	@SuppressWarnings("unchecked")
	public List<Alert> list(Page page){
		
		return dao.findByPage(Alert.class, null, new Sort("updateTime", "desc"), page);
	}
	
	@Transactional
	public String  add(Alert newAlert){
		return dao.save(newAlert);
	}
	
	public void update(Alert alert){
		
		dao.update(alert);
	}
	
	@Transactional(readOnly=false)
	public void delete(String id){
		dao.deleteById(Alert.class,id);
	}

	@Override
	public Alert getBy(String alertId) {
		// TODO Auto-generated method stub
		return (Alert) dao.get(Alert.class, alertId);
	}

	@Override
	public Long getCount() {
		// TODO Auto-generated method stub
		return dao.getCount("from Alert c");
	}
}
